#include "ThreadLockFreeQueue.h"
#include <thread>
#include <future>
#include <iostream>

class People
{
public:
	People(){};
	~People(){};
    int a;
};

void AddData(ThreadLockFreeQueue<People>* t)
{
	for (int i = 0; i < 1000000; i++) {
		People* p = new People();
		while (!t->AddTail(p)) {
		}
	}
	std::cout << "AddData 1000000 ok" << std::endl;
}

void Pop(ThreadLockFreeQueue<People>* t)
{
	for (int i = 0; i < 2000000; i++) {
		People* p = NULL;
		while (!t->GetFirst(&p)) {
		}
		delete p;
	}
	std::cout << "pop 2000000 ok" << std::endl;
}

int main()
{
    // 创建大小1000的队列
	ThreadLockFreeQueue<People> qs(1000);
    // 创建4个写入线程
	auto fut1 = std::async(&AddData, &qs);
	auto fut2 = std::async(&AddData, &qs);
	auto fut3 = std::async(&AddData, &qs);
	auto fut4 = std::async(&AddData, &qs);
    // 创建2个读取线程
    auto fut5 = std::async(&Pop, &qs);
	std::thread t(&Pop, &qs);
    // 执行
	t.join();
	fut1.wait();
	fut2.wait();
	fut3.wait();
	fut4.wait();
    fut5.wait();
	return 0;
}

